<script language="javascript" type="text/javascript">
	function validateForm() {

	var title = document.getElementById("title");
		title.style.backgroundColor="#fff";
		if(title.value == ''){
		alert('Vui lòng nhập tiêu đề!');
		title.focus();
		title.style.backgroundColor="#F90";
		return false;
		}
	}
</script>
<?php
	$id=$_GET['id'];
	$sql="select * from dm_sanpham_loai where id=".$id;
	$rs=mysql_query($sql);
	$row=mysql_fetch_assoc($rs);

	if(isset($_POST['submit']))
	{
		$title=$_POST['title'];				
		$state=$_POST['state'];		
		$ordering=$_POST['ordering'];
		
		if(isset($id) && $id >0)
		{
			$sql="update dm_sanpham_loai set 
									   title='$title',									 
									   state='$state',
									   ordering='$ordering'									  
										where id=".$id;
			// echo $sql;
			mysql_query($sql);
			echo'<script>alert("Cập nhật loại sản phẩm thành công");window.location="?action=sanpham_loai";</script>';
			//update
			
		}else{
			
			$sql="INSERT INTO `dm_sanpham_loai` (`title`, `state`, `ordering`) VALUES ('$title', '$state', '$ordering')";
			mysql_query($sql);
			echo'<script>alert("Thêm loại sản phẩm thành công");window.location="?action=sanpham_loai";</script>';

		}
		
	}
?>

<!-- Container -->
<div id="container">
	<div class="shell">

		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Thêm loại sản phẩm</h2>
					</div>
					<!-- End Box Head -->
					
					<form action="#" id="frmcontent" method="post" enctype="multipart/form-data" onSubmit="return validateForm();">
						
						<!-- Form -->
						<div class="form">
								<p>
									<label>Tên loại sản phẩm<span> (*)</span></label>
									<input type="text" name="title" id="title" value="<?php echo $row['title'] ?>" class="field size1" />
								</p>
						</div>
										
				</div>
				<!-- End Box -->

			</div>
			<!-- End Content -->
			
			<!-- Sidebar -->
			<div id="sidebar">
				
				<!-- Box -->
				<div class="box">
					
					<!-- Box Head -->
					<div class="box-head">
						<h2>Chức năng</h2>
					</div>
					<!-- End Box Head-->

					<div class="box-content">
						<div class="buttons">
							<input type="button" onclick="window.location.href='?action=sanpham_loai'" class="button" value="Thoát" />
							<!--<input type="button" onclick="validateForm()" name="submit" class="button" value="Lưu" />-->
							<input type="submit"  name="submit" class="button" value="Lưu" />
						</div>
	
						
						<!-- Sort -->
						<div class="sort">								
							<label>Thứ tự</label>
							<input type="text" name="ordering" value="<?php echo $row['ordering'] ?>" class="field" />
							
							<div style="margin:16px;  background-color: gainsboro; height: 17px;">
							<?php
							if(!isset($row['state']))
							{
								$checked0 = "";
								$checked1 = "CHECKED";
							}else{
								if($row['state']==1)
								{
									$checked0 = "";
									$checked1 = "CHECKED";
								}else{
									$checked0 = "CHECKED";
									$checked1 = "";
								}
							}
							?>
							<span style="margin-right:10px; margin-left: 20px;">Bật</span><INPUT TYPE=radio <?php echo $checked1?>  NAME="state" VALUE="1">&nbsp &nbsp &nbsp &nbsp &nbsp
							<span style="margin-right:10px">Tắt</span><INPUT TYPE=radio <?php echo $checked0?> NAME="state" VALUE="0">
							</div>												
						</div>
						<!-- End Sort -->
						
					</div>
				</div>
				<!-- End Box -->
			</div>
			<!-- End Sidebar -->
			</form>
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->
<script>
	var editor=CKEDITOR.replace( 'fulltext' );
	CKFinder.setupCKEditor( editor, '../libs/ckfinder/' ) ;

</script>
